﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Domain = ABCApp.Domain;

using System.Data;
using System.Data.Common;
using System.Resources;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace ABCApp.DAL
{
    public class Customer : BaseDAL
    {
        public Domain.Customer GetCustomerById(int customerId)
        {
            IDataReader dr = null;
            IList<Domain.Customer> lstCustomers = null;

            string sql = "select customerid, firstname, middlename, lastname, companyname, emailaddress, phone, modifieddate from saleslt.customer where customerid = @customerid";

            DbCommand cmd = db.GetSqlStringCommand(sql);

            db.AddInParameter(cmd, "customerid", DbType.Int32, customerId);

            try
            {
                dr = db.ExecuteReader(cmd);
                lstCustomers = AdaptToList(dr);
            }
            catch (System.Data.SqlClient.SqlException exsql)
            {
                throw new Exception(exsql.Message);
            }
            catch
            {
            }
            finally
            {
                if ((dr != null) && (!dr.IsClosed))
                    dr.Close();
            }

            return ((lstCustomers != null) || (lstCustomers[0] != null)) ? lstCustomers[0] : null;
        }

        public IList<Domain.Customer> GetCustomers()
        {
            IDataReader dr = null;
            IList<Domain.Customer> lstCustomers = null;

            string sql = "select customerid, firstname, middlename, lastname, companyname, emailaddress, phone, modifieddate from saleslt.customer";

            DbCommand cmd = db.GetSqlStringCommand(sql);

            try
            {
                dr = db.ExecuteReader(cmd);
                lstCustomers = AdaptToList(dr);
            }
            catch (System.Data.SqlClient.SqlException exsql)
            {
                throw new Exception(exsql.Message);
            }
            finally
            {
                if ((dr != null) && (!dr.IsClosed))
                    dr.Close();
            }

            return lstCustomers;
        }

        public IList<Domain.Customer> AdaptToList(IDataReader dr)
        {
            List<Domain.Customer> lstCustomers = new List<Domain.Customer>();

            while (dr.Read())
            {
                Domain.Customer c = new Domain.Customer();

                c.CompanyName = dr["companyname"].ToString();
                c.CustomerId = Convert.ToInt32(dr["customerid"]);
                c.EmailAddress = dr["emailaddress"].ToString();
                c.FirstName = dr["firstname"].ToString();
                c.LastName = dr["lastname"].ToString();
                c.MiddleName = dr["middlename"].ToString();
                c.ModifiedDate = Convert.ToDateTime(dr["modifieddate"]);
                c.Phone = dr["phone"].ToString();

                lstCustomers.Add(c);
            }

            return lstCustomers;
        }
    }
}
